package cn.kgc.mes.salse.service.impl;

import java.math.BigDecimal;
import java.util.List;
import cn.kgc.mes.common.utils.DateUtils;
import cn.kgc.mes.productreceptline.domain.ProductRecptLine;
import cn.kgc.mes.productreceptline.mapper.ProductRecptLineMapper;
import cn.kgc.mes.saleline.domain.ProductSalseLine;
import cn.kgc.mes.saleline.service.IProductSalseLineService;
import cn.kgc.mes.storage.domain.MaterialStock;
import cn.kgc.mes.storage.service.IMaterialStockService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.kgc.mes.salse.mapper.ProductSalseMapper;
import cn.kgc.mes.salse.domain.ProductSalse;
import cn.kgc.mes.salse.service.IProductSalseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售出库单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-21
 */
@Service
public class ProductSalseServiceImpl extends ServiceImpl<ProductSalseMapper, ProductSalse> implements IProductSalseService
{
    @Autowired
    private ProductSalseMapper productSalseMapper;
    @Autowired
    private IProductSalseLineService productSalseLineService;
    @Autowired
    private IMaterialStockService materialStockService;
    /**
     * 查询销售出库单
     * 
     * @param salseId 销售出库单主键
     * @return 销售出库单
     */
    @Override
    public ProductSalse selectProductSalseBySalseId(Long salseId)
    {
        return productSalseMapper.selectProductSalseBySalseId(salseId);
    }

    /**
     * 查询销售出库单列表
     * 
     * @param productSalse 销售出库单
     * @return 销售出库单
     */
    @Override
    public List<ProductSalse> selectProductSalseList(ProductSalse productSalse)
    {
        return productSalseMapper.selectProductSalseList(productSalse);
    }

    /**
     * 新增销售出库单
     * 
     * @param productSalse 销售出库单
     * @return 结果
     */
    @Override
    public int insertProductSalse(ProductSalse productSalse)
    {
        productSalse.setCreateTime(DateUtils.getNowDate());
        return productSalseMapper.insertProductSalse(productSalse);
    }

    /**
     * 修改销售出库单
     * 
     * @param productSalse 销售出库单
     * @return 结果
     */
    @Override
    public int updateProductSalse(ProductSalse productSalse)
    {
        productSalse.setUpdateTime(DateUtils.getNowDate());
        return productSalseMapper.updateProductSalse(productSalse);
    }

//    /**
//     * 批量删除销售出库单
//     *
//     * @param salseIds 需要删除的销售出库单主键
//     * @return 结果
//     */
//    @Override
//    public int deleteProductSalseBySalseIds(Long[] salseIds)
//    {
//        return productSalseMapper.deleteProductSalseBySalseIds(salseIds);
//    }

    /**
     * 删除销售出库单信息
     * 
     * @param salseId 销售出库单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteProductSalseBySalseId(Long salseId)
    {
        return  productSalseMapper.deleteProductSalseBySalseId(salseId);
    }
    @Override
    @Transactional
    public int outProductSalseBySalseId(Long salseId)
    {
        int i = 0;
        //查询当前的出货单数量
        LambdaQueryWrapper<ProductSalseLine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSalseLine::getSalseId,salseId);
        //得到出货单列表
        List<ProductSalseLine> list = productSalseLineService.list(queryWrapper);
        for (ProductSalseLine productSalseLine : list) {
            //所有出货单行数量
            BigDecimal quantitySalse = productSalseLine.getQuantitySalse();
            System.out.println("出货单行数量"+quantitySalse);
            System.out.println("出货物料id"+productSalseLine.getItemId());
            //根据当前货物id查询库存
            LambdaQueryWrapper<MaterialStock> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(MaterialStock::getItemId,productSalseLine.getItemId());
            MaterialStock materialStock = materialStockService.getOne(queryWrapper2);
            BigDecimal quantityOnhand = materialStock.getQuantityOnhand();
            System.out.println("库存数量"+quantityOnhand);
            //计算库存的数量
            Integer quantityOnhand1 = quantityOnhand.intValue()-quantitySalse.intValue();
            //更新库存数量
            materialStock.setQuantityOnhand(new BigDecimal(quantityOnhand1));
            boolean a = materialStockService.updateById(materialStock);
            if (a){
                i=1;
            }
        }
        return i;
    }
}
